package com.example.bysj_sb.dao;

import com.example.bysj_sb.domain.GraduateProjectType;
import com.example.bysj_sb.domain.School;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("graduateProjectTypeDao")
public class GraduateProjectTypeDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    //增加
    public boolean add(GraduateProjectType graduateProjectType) {
        String sql = "INSERT INTO graduateProjectType (no,description,remarks) VALUES (?,?,?)";
        int affectedRowNum = jdbcTemplate.update(sql, graduateProjectType.getNo(), graduateProjectType.getDescription(),graduateProjectType.getRemarks());
        if (affectedRowNum != 1) {
            return false;
        } else {

            return true;
        }
    }

    public GraduateProjectType find(int id){
        String sql = "SELECT * FROM graduateProjectType WHERE id = ?";
        List<GraduateProjectType> graduateProjectTypes = jdbcTemplate.query(sql, new Object[]{id},
                new BeanPropertyRowMapper<>(GraduateProjectType.class));
        if(!graduateProjectTypes.isEmpty()){
            return graduateProjectTypes.get(0);
        } else
            return null;
    }
    public List<GraduateProjectType> findAll(){
        String sql = "SELECT * FROM GraduateProjectType";

        List<GraduateProjectType> graduateProjectTypes = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(GraduateProjectType.class){

        });
        return graduateProjectTypes;
    }
    public boolean update(GraduateProjectType graduateProjectType){
        String sql = "UPDATE GraduateProjectType SET no = ? ,description=? WHERE id = ?";
        int affectedRowNum = jdbcTemplate.update(sql,graduateProjectType.getNo(),graduateProjectType.getDescription(),graduateProjectType.getId());
        if (affectedRowNum!=1){
            return false;
        }else{
            return true;
        }
    }
    public boolean delete(int id){
        String sql = "DELETE FROM GraduateProjectType WHERE id = ? ";
        int affectedRowNum = jdbcTemplate.update(sql,id);
        if (affectedRowNum!=1){
            return false;
        }else{
            return true;
        }
    }
}
